/**
  * Copyright (c) CSIRO Australia, 2009
  *
  * @author $Author: jia020 $
  * @version $Id: Tester.java 643 2009-05-20 00:20:55Z jia020 $
  */
package au.csiro.nt.pdsp.util;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;



public class Tester
{
	public static void main(String args[])
	{
// 		if (args.length == 0)
// 		{
// 			System.out.println(GroupXmlSignatureUtil.signString("C:\\home\\cwang\\dev\\workspace\\econtract\\conf\\partya.keystore", 
// 					"tester", 
// 					"key-party-b", 
// 					"<contract><participant>partya</participant><participant>partyb</participant></contract>"));
//			
// 			return;
// 		}
		
		if (args.length == 0)
		{
			String signed_doc = GroupXmlSignatureUtil.signString("C:\\home\\cwang\\dev\\workspace\\econtract\\conf\\party-x.store", 
					"tester", 
					"mykey", 
					"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><contract><participant><name>partya</name><role>provider</role></participant><add>nothing</add></contract>");
			
			System.out.println(GroupXmlSignatureUtil.signString("C:\\home\\cwang\\dev\\workspace\\econtract\\conf\\partya.keystore", 
					"tester", 
					"key-party-b", 
					signed_doc));
			return;
		}

//		if (args.length == 0)
//		{
//			String tmp = "<contract><participant><name>partya</name><role>provider</role><Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\">";
//			tmp += "<SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\"/><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"/>";
//			tmp += "<Reference URI=\"test\"><Transforms><Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\"/></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"/><DigestValue>0R3SEa3SNrm8ylS0jvP4K9vsItc=</DigestValue></Reference></SignedInfo><SignatureValue>2oA1dxQoqqWQk7/3FOhScRy8aF6viOEPJ8LoBAPoLHfpDAkUS5tJ8DrcbOV8+YViHvOuZqTiTc4pWuf9DOaF6d50WM9LDjEJK9DnCyJls6x09M4KPBVzw+SnkXfq1V3g/88ARDT1Jf49AXnCiMBb69+GnQPdJZVLUVAAoDQTJEM=</SignatureValue><KeyInfo><X509Data><X509SubjectName>CN=a,OU=b,O=c,L=sydney,ST=nsw,C=au</X509SubjectName><X509Certificate>MIICFzCCAYCgAwIBAgIESAg3sDANBgkqhkiG9w0BAQUFADBQMQswCQYDVQQGEwJhdTEMMAoGA1UECBMDbnN3MQ8wDQYDVQQHEwZzeWRuZXkxCjAIBgNVBAoTAWMxCjAIBgNVBAsTAWIxCjAIBgNVBAMTAWEwHhcNMDgwNDE4MDU1NDU2WhcNMDgwNzE3MDU1NDU2WjBQMQswCQYDVQQGEwJhdTEMMAoGA1UECBMDbnN3MQ8wDQYDVQQHEwZzeWRuZXkxCjAIBgNVBAoTAWMxCjAIBgNVBAsTAWIxCjAIBgNVBAMTAWEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAN2DezDcNEG02lUB6kYzpCtHg8+9D8m/1R1ZbXaTMX/n7Hxe9BEw6mYAl+yftTwZxcDNlqqyXdRvmqOvg5YU76hzeS633HKEcfL7bi7vk6YqKYj7HBuhar89o1NEMJSgCx74l3YnU6nV0oR3drZPxZzLhWm61xzUo63x8LtlYUHvAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAsW/vd9Q/2Gse1CUL69hiqCPpFFVxChmv6NGje9oKbkc64HUrNrZFNBTbGJbEvVe10GvVZ/sDQ32ajbj9oK0FC4WTY4CpzTeiOsCt2vqft/kRh/4klg1U7vDlFhRbz61q2Vf2Qk0+SEgXMH7wNPtcfHK3cxzpw7ddj6RY7BhDMyE=</X509Certificate></X509Data></KeyInfo></Signature></participant><add>nothing</add></contract>";
//			System.out.println(GroupXmlSignatureUtil.signString("C:\\home\\cwang\\dev\\workspace\\econtract\\conf\\party-x.store", 
//					"tester", 
//					"mykey", 
//					tmp));
//			return;
//					
//		}
		
		validateSignedDocument(args[0]);
	
	}

	public static void validateSignedDocument(String theFilePath)
	{
		Document doc;
		try
		{
			doc = XmlUtil.fileToDocument(theFilePath);
			if (!GroupXmlSignatureUtil.validateDocument(doc))
				System.out.println("failed to validate document "+theFilePath);
			else
				System.out.println("document "+theFilePath+" is valid!");
		} catch (SAXException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (ParserConfigurationException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
